package org.smart4j.framework.helper;

import org.apache.log4j.helpers.ThreadLocalMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Wangminze
 * @version 2016/12/26
 * Description : Servlet助手类,用于Action 与 ServletAPI解耦
 */
public final class ServletHelper {

    private static final Logger LOGGER
            = LoggerFactory.getLogger(ServletHelper.class);

    /** 使每个线程独自拥有一份ServletHelper实例 **/
    private static final ThreadLocal<ServletHelper> SERVLET_HELPER_HOLDER
            = new ThreadLocal<ServletHelper>();

    private HttpServletRequest request;
    private HttpServletResponse response;

    public ServletHelper(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    /**
     * Description : 初始化
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param request
     * @param response
     **/
    public static void init(HttpServletRequest request, HttpServletResponse response){
        SERVLET_HELPER_HOLDER.set(new ServletHelper(request, response));
    }

    /**
     * Description : 销毁
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    public static void destory(){
        SERVLET_HELPER_HOLDER.remove();
    }

    /**
     * Description : 获取Request对象
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    private static HttpServletRequest getRequest(){
        return SERVLET_HELPER_HOLDER.get().request;
    }

    /**
     * Description : 获取Response对象
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    private static HttpServletResponse getResponse(){
        return SERVLET_HELPER_HOLDER.get().response;
    }

    /**
     * Description : 获取Session对象
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    private static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * Description : 获取ServletContext对象
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    private static ServletContext getServletContext(){
        return getRequest().getServletContext();
    }

    /**
     * Description : 将属性放入Request 中
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @param value
     * @return :
     **/
    public static void setRequestAttribute(String key, Object value){
        getRequest().setAttribute(key, value);
    }

    /**
     * Description : 从Request中获取属性
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @return
     **/
    @SuppressWarnings("unchecked")
    public static <T> T getRequestAttribute(String key){
        return (T) getRequest().getAttribute(key);
    }

    /**
     * Description : 从Request中移除属性
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @return
     **/
    public static void removeRequestAttribute(String key){
        getRequest().removeAttribute(key);
    }

    /**
     * Description : 发送从定向响应
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param location
     * @return
     **/
    public static void sendRedirect(String location){
        try {
            getResponse().sendRedirect(getRequest().getContextPath() + location);
        }catch (IOException e){
            LOGGER.error("Redirect failure", e);
        }
    }

    /**
     * Description : 将属性放入 Session 中
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @param value
     * @return
     **/
    public static void setSessionAttribute(String key, Object value){
        getSession().setAttribute(key, value);
    }

    /**
     * Description : 从Session中获取属性
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @return
     **/
    @SuppressWarnings("unchecked")
    public static <T> T getSessionAttribute(String key){
        return (T) getSession().getAttribute(key);
    }

    /**
     * Description : 从Session中移除属性
     * @antuor : Wangminze
     * @version : 2016/12/26
     * @param key
     * @return
     **/
    public static void removeSessionAttribute(String key){
        getSession().removeAttribute(key);
    }

    /**
     * Description : 使Session失效
     * @antuor : Wangminze
     * @version : 2016/12/26
     **/
    public static void invalidateSession(){
        getRequest().getSession().invalidate();
    }
}
